﻿package  {
	
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.events.Event;
	import flash.system.fscommand;
	import com.as3toolkit.ui.Keyboarder;
	
	
	public class Main extends MovieClip {
		
		/*
		* Used for update logic, in current
		* testing build won't be too relevant.
		*
		* 0 - Title Screen
		* 1 - Main Screen
		* 2 - Loading Screen
		* 3 - Cutscene
		* 4 - Battle Select
		* 5 - Saving Screen
		* 6 - Battle
		* 7 - Paused in battle
		*/
		var currentScreen:int;
		var Title:TitleScreen;
		//Perhaps one cutscene symbol with all cutscenes within it (gotoAndPlay with frame labels)
		//Or else a vector of cutscenes? Needs thought. Either way, rather expensive.
		var cutScene:cut1;
		var misSelect:MissionSelect;
		public var kb:Keyboarder;
		public var save:SaveFile;
		
		
		public function Main() {
			kb = new Keyboarder(this);
			currentScreen = 0;
			Title = new TitleScreen();
			cutScene = new cut1();
			misSelect = new MissionSelect();
			//addChild(Title);
			Title.Button1.addEventListener(MouseEvent.CLICK, beginGame);
			Title.Button4.addEventListener(MouseEvent.CLICK, quit);
			misSelect.quit.addEventListener(MouseEvent.CLICK, returntoTitle);
		}
		
		public function beginGame(e:MouseEvent):void
		{
			removeChild(Title);
			if(save == null)
			{
			addChild(cutScene);
			cutScene.gotoAndPlay(1);
			//Should probably go in 'update' logic
			cutScene.addEventListener(Event.ENTER_FRAME, checkforCompletion);
			}
			else
			{
				addChild(misSelect);
				for(var i:int = 0; i < save.currentLevel; i++)
				{
					misSelect.missionComplete(i);
				}
				misSelect.unlock(save.currentLevel);
			}
		}
		
		public function checkforCompletion(e:Event):void
		{
			if(cutScene.currentFrameLabel == "Done")
			{
				cutScene.gotoAndStop(1);
				removeChild(cutScene);
				addChild(misSelect);
				currentScreen = 4;
				misSelect.unlock(0);
			}
		}
		
		public function quit(e:MouseEvent):void
		{
			fscommand("quit");
		}
		
		public function returntoTitle(e:MouseEvent):void
		{
			currentScreen = 0;
			misSelect.cleanup();
			removeChild(misSelect);
			addChild(Title);
			
		}
	}
	
}
